<template>
  <div class="dialog-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="addSelectCars"
          v-hasPermi="['basic:routeVehicle:add']">添加已有车辆</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['basic:routeVehicle:add']">新增车辆</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['basic:routeVehicle:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="selectDel"
          v-hasPermi="['basic:routeVehicle:remove']">移除选中车辆</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" @click="handleDelete"
          v-hasPermi="['basic:routeVehicle:remove']">移除全部车辆</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="routeVehicleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车牌号" align="center" prop="plateNo" />
      <el-table-column label="车号" align="center" prop="numbered" />
      <el-table-column label="车牌颜色" align="center" prop="color" >
        <template #default="scope">
          <dict-tag :options="basic_vehicle_color" :value="scope.row.color" />
        </template>
        </el-table-column>
      <el-table-column label="车辆类型" align="center" prop="type" >
        <template #default="scope">
          <dict-tag :options="basic_vehicle_type" :value="scope.row.type" />
        </template>
        </el-table-column>
      <el-table-column label="车辆运营状态" align="center" prop="outageStatus" >
        <template #default="scope">
          <dict-tag :options="basic_vehicle_outage_status" :value="scope.row.outageStatus" />
        </template>
        </el-table-column>
      <el-table-column label="车辆状态 " align="center" prop="status" >
        <template #default="scope">
          <dict-tag :options="sys_data_status" :value="scope.row.status" />
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改线路车辆对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <EditVehicle ref="vehicleRef" :route-id="props.routeId" :form="form" @op-end="listenSubmitForm" />
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog :title="vehicleTitle" v-model="vehicleOpen" width="600px" append-to-body>
      <AddVehicleList :route-id="props.routeId" @op-end="listenAdd" />
    </el-dialog>
  </div>
</template>

<script setup name="RouteVehicle">
import EditVehicle from '@/views/basic/vehicle/components/edit'
import AddVehicleList from './components/index.vue'
import { listRouteVehicle, delRouteVehicle,delVehicle } from "@/api/basic/routeVehicle";
import { getVehicleList,getVehicle as getVehicleDetail  } from "@/api/basic/vehicle";
import { ref } from "vue";
const { proxy } = getCurrentInstance();

const { basic_vehicle_color, basic_vehicle_type, basic_vehicle_outage_status, sys_data_status } = proxy.useDict('basic_vehicle_color', 'basic_vehicle_type', 'sys_industry', 'basic_vehicle_outage_status', 'sys_data_status');
const props = defineProps({
  // 当前的值
  routeId: [Number, String],
  deptId: [Number, String],
});
const routeVehicleList = ref([]);
const open = ref(false);
const title = ref("");
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);


const data = reactive({
  form: {},
  queryParams: {
    routeId: undefined,
    vehicleId: undefined
  },
  rules: {
    vehicleId: [
      { required: true, message: "请选择车辆", trigger: "change" }
    ],
    color: [
      { required: true, message: "请选择车牌颜色", trigger: "change" }
    ],
    transportType: [
      { required: true, message: "请选择行业类别", trigger: "change" }
    ],
    type: [
      { required: true, message: "请选择车辆类型", trigger: "change" }
    ],
    outageStatus: [
      { required: true, message: "请选择停运状态", trigger: "change" }
    ],
    netState: [
      { required: true, message: "请选择联网联控状态", trigger: "change" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询线路车辆列表 */
function getList() {
  loading.value = true;
  listRouteVehicle(queryParams.value).then(response => {
    routeVehicleList.value = response.data; 
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    routeId: props.routeId,
    vehicleId: null,
    plateNo: null,
    numbered: null,
    color: null,
    type: null,
    netState: null,
    operationRoute: null,
    transportType: null,
    outageStatus: null,
    status: null,
    deptId: props.deptId,
  };
  proxy.resetForm("routeVehicleRef");
}


// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.vehicleId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加线路车辆";
}

/** 修改按钮操作 */
function handleUpdate(row) {
 
  loading.value = true
  reset();
  const vehicleId = row.vehicleId || ids.value
  getVehicleDetail(vehicleId).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改线路车辆";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["vehicleRef"].submitForm();
  return false;
}

function listenSubmitForm(obj){
  if(obj.status=='success'){
    open.value = false;
    getList();
  }else{
    buttonLoading.value = false;
  }
}

/** 删除已选车辆按钮操作 */
function selectDel(){
  let delParams = [];
  ids.value.forEach((vehicleId)=>{
    delParams.push({routeId:props.routeId,vehicleId:vehicleId})
  })
  proxy.$modal.confirm('是否删除所选"' + ids.value.length + '"辆车？').then(function () {
    loading.value = true;
    return delVehicle(delParams)
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
  
}

/** 删除线路全部车辆按钮操作 */
function handleDelete() {
  const _routeIds = props.routeId
  proxy.$modal.confirm('是否确认删除线路编号为"' + _routeIds + '"的所有车辆数据？').then(function () {
    loading.value = true;
    return delRouteVehicle(_routeIds);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}
watch(props, (newVal, oldVal) => {
  queryParams.value.routeId = newVal.routeId
  getList()
}, { immediate: true });
const vehicleList = ref([]);
function getVehicle() {
  getVehicleList().then(res => {
    vehicleList.value = res.rows
  })
}
getVehicle()
function selectVehicle(e) {
  console.log(e.split('_'))
  let info = e.split('_')
  form.value.vehicleId = info[0]
  form.value.plateNo = info[1]
  form.value.numbered = info[2]
}
const selectedVehicle = computed(() => {
  return form.value.vehicleId ? form.value.vehicleId + '_' + form.value.plateNo + '_' + form.value.numbered:""
})

const vehicleOpen = ref(false);
const vehicleTitle = ref("");
function addSelectCars(){
  vehicleOpen.value = true;
  vehicleTitle.value = '添加已有车辆'
}
function listenAdd(obj){
  if(obj.status=='success'){
    vehicleOpen.value = false;
    getList();
  }
}
</script>
